package model.cell.tower;

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Composite;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.io.IOException;

import javax.imageio.ImageIO;

import model.bullet.AbstractBullet;
import model.bullet.LaserBullet;
import model.enemy.AbstractEnemy;
import model.game.EnemyWave;


public class LaserTower extends AbstractTower {

	protected int _powerMax = 100;
	
	protected int _power = 0;
	
	protected int powerLoadSpeed = 10;
	
	
	public LaserTower(Point pos, Dimension dim, EnemyWave enemyWave) {
		super(pos, dim, enemyWave);
		_fireInterval = 50;
		_range = 80;
		color = Color.GREEN;
		
		try {
			_img = ImageIO.read(getClass().getResource("/towerGreen.png"));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	

	private AlphaComposite makeComposite(float alpha) {
		
		int type = AlphaComposite.SRC_OVER;
		return(AlphaComposite.getInstance(type, alpha));
	}

	
	protected void _drawTower(Graphics g) {
		
		super._drawTower(g);
		
	    int radius = (int) ((_power * 10) / _powerMax);
	    
	    if (0 < radius ) {
	    	
	    	float alpha = (float) ((_power * 0.5) / _powerMax);
	    	Graphics2D g2d = (Graphics2D) g;
	    	Composite originalComposite = g2d.getComposite();
			g2d.setComposite(makeComposite(alpha));

	    	g2d.setPaint(Color.GREEN);
		    g2d.fillRect(_rect.x + (_rect.width / 2) - radius, _rect.y + (_rect.height / 2) - radius, 2*radius + 1, 2*radius + 1);
		    
		    g2d.setComposite(originalComposite);
	    }

	}
	
	
	protected AbstractBullet _createBullet(AbstractEnemy enemy) {
		
		if ( _power == _powerMax ) {
			LaserBullet bullet = new LaserBullet(new Point(_rect.x + _rect.width / 2, _rect.y + _rect.height / 2), enemy, this);
			bullet.setDamage(_power);
			_power = 0;
			return bullet;
		}
		
		_incPower();
		return null;
	}
	
	
	public void _incPower() {
		
		if (_bullets.size() > 0) return;
		
		_power += powerLoadSpeed;
		
		if ( _power > _powerMax ) {
			_power = _powerMax;
		}
	}
	
}
